<template>
    <div class="header">
        <!-- 新增: 左右装饰背景容器（用户需自行替换图片路径） -->
        <div class="decor-bg left">
            <!-- 左侧波浪装饰图（用户替换src） -->
            <img src="../../assets/HeadBackground.png" 
                 alt="left-decor" 
                 style="height: 120%;transform: rotate(-10deg);">
        </div>
        <div class="decor-bg right">
            <!-- 右侧气泡装饰图（用户替换src） -->
            <img src="../../assets/HeadBackground.png" 
                 alt="right-decor" 
                 style="height: 110%;transform: rotate(15deg);">
        </div>
        <div class="header-container">
            <div class="app-name">
                <router-link to="/">
                    <img src="../../assets/lyh.png" style="width: 40px;position: relative; top: 13px;right: 6px">
                    <b style="color: #409EFF" >
                        校园二手交易市场
                    </b>
                </router-link>
            </div>
            <div class="search-container">
                <el-input placeholder="搜物品..." v-model="searchValue" @keyup.enter.native="searchIdle">
                    <el-button slot="append" icon="el-icon-search" @click="searchIdle"></el-button>
                </el-input>
            </div>
            <el-button type="primary" icon="el-icon-plus"  @click="toRelease">物品发布</el-button>
            <el-button type="primary" icon="el-icon-chat-dot-round" @click="toMessage">消息</el-button>
            <router-link v-if="!isLogin" class="user-name-text" to="/login">登录</router-link>
            <el-dropdown trigger="click" v-else>
                <div style="cursor:pointer;display: flex;align-items: center;">
                    <div style="font-size: 16px;color: #409EFF;padding-right: 5px;">{{nicknameValue?nicknameValue:nickname}}</div>
                    <el-avatar :src="avatarValue?avatarValue:avatar"></el-avatar>
                </div>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item><div @click="toMe">个人中心</div></el-dropdown-item>
                    <el-dropdown-item divided style="color: red;"><div @click="loginOut">退出登录</div></el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>
<script>
import { forceLogout } from '@/utils/request';
    export default {
        name: 'Header',
        props: ['searchInput','nicknameValue','avatarValue'],
        data() {
            return {
                searchValue: this.searchInput,
                nickname:'登录',
                avatar:this.avatar||'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
                isLogin:false
            };
        },
        created(){
            // console.log("header");
            if(! this.$globalData.userInfo.nickname){
                this.$api.getUserInfo().then(res=>{
                    console.log('Header getUserInfo:',res);
                    if(res.status_code===1)
                    {
                        console.log('User getUserStatus:',res.userStatus);
                        //判断是否为初始账号
                        if(res.data.userStatus===3)
                        {
                            this.$confirm('检测到您使用的是初始密码 123456，请立即修改密码以保障账户安全。', '安全提示', {
                            confirmButtonText: '去修改',
                            cancelButtonText: '稍后再说',
                            type: 'warning'
                        }).then(() => {
                            this.$router.push('/me'); // 跳转到修改密码页
                        }).catch(() => {});
                        }   
                        this.nickname=res.data.nickname;
                        this.avatar=res.data.avatar;
                        res.data.signInTime=res.data.signInTime.substring(0,10);
                        this.$globalData.userInfo=res.data;
                        this.isLogin=true;
                    }
                })
            }else {
                this.nickname=this.$globalData.userInfo.nickname;
                this.avatar=this.$globalData.userInfo.avatar;
                this.isLogin=true;
            }
        },
        methods: {
            searchIdle() {
                if ('/search' !== this.$route.path) {
                    this.$router.push({path: '/search', query: {searchValue: this.searchValue}});
                } else {
                    this.$router.replace({path: '/search', query: {searchValue: this.searchValue}});
                    this.$router.go(0);
                }

            },
            toMe() {
                if ('/me' !== this.$route.path) {
                    this.$router.push({path: '/me'});
                }
            },
            toMessage(){
                if ('/message' !== this.$route.path) {
                    this.$router.push({path: '/message'});
                }
            },
            toRelease(){
                if ('/release' !== this.$route.path) {
                    this.$router.push({path: '/release'});
                }
            },
            loginOut() {
                forceLogout(); // ✅ 正确调用
                    }
        }
    };
</script>
<style scoped>
    /* 新增闲鱼主题变量 */
    :root {
      --xy-primary: #ff5500; /* 闲鱼主题橙 */
      --xy-hover: #ff7733;   /* 悬停色 */
      --xy-bg: #f8f8f8;     /* 背景灰 */
    }
     /* 新增装饰背景样式 */
     .decor-bg {
        position: absolute;
        top: 0;
        height: 100%;
        z-index: 0; /* 置于内容层下方 */
        opacity: 0.8; /* 降低透明度避免喧宾夺主 */
    }
    .decor-bg.left {
        left: -50px;
        width: 300px;
    }
    .decor-bg.right {
        right: -80px;
        width: 250px;
    }
    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 72px; /* 增加高度容纳装饰 */
        overflow: hidden; /* 隐藏装饰图片溢出部分 */
        z-index: 1000;
        background: linear-gradient(to right, 
            rgba(255,235,235,0.9) 0%, 
            rgba(255,255,255,0.95) 30%, 
            rgba(255,255,255,0.95) 70%, 
            rgba(235,255,245,0.9) 100%
        ); /* 轻量渐变色背景 */
    }

    .header img[src*="HeadBackground"] {
        display: none; /* 隐藏原有背景图 */
    }

    .header-container {
        position: relative;
        z-index: 2; /* 确保内容在装饰背景之上 */
        width: 1200px; /* 加宽容器 */
        height: 100%;
        display: flex;
        align-items: center;
        gap: 24px; /* 元素间距 */
    }

    .app-name a {
        text-shadow: 1px 1px 2px rgba(255,85,0,0.1); /* 文字投影 */
        color: var(--xy-primary) !important;
        font-size: 26px;
        font-weight: 700;
        text-decoration: none;
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .app-name img {
        width: 48px !important;
        top: 0 !important;
        right: 0 !important;
        filter: drop-shadow(0 2px 4px rgba(255,85,0,0.2)); /* logo投影 */
    }

    /* 搜索框改造 */
    .search-container {
        flex-grow: 1;
        max-width: 500px;
    }

    .search-container >>> .el-input__inner {
        border-radius: 20px !important;
        border: 2px solid var(--xy-primary) !important;
        background: var(--xy-bg) !important;
        height: 40px !important;
    }

    .search-container >>> .el-input-group__append {
        background: var(--xy-primary) !important;
        border-radius: 0 20px 20px 0 !important;
        border: none !important;
    }

    .search-container >>> .el-icon-search {
        color: black !important;
        font-size: 18px;
    }

    /* 按钮改造 */
    .el-button--primary {
        color: black !important; /* 新增这行 */
        background: var(--xy-primary) !important;
        border-color: var(--xy-primary) !important;
        border-radius: 8px !important;
        padding: 10px 20px !important;
        font-weight: 500 !important;
        transition: all 0.3s !important;
    }

    .el-button--primary:hover {
        background: var(--xy-hover) !important;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(255,85,0,0.3) !important;
    }
            /* 新增按钮装饰细节 */
    .el-button--primary::after {
        content: '';
        position: absolute;
        top: -5px;
        right: -5px;
        width: 16px;
        height: 16px;
        background: url('../../assets/HeadBackground.png') no-repeat; /* 用户可添加装饰图标 */
        background-size: contain;
    }
    /* 用户信息区域 */
    .user-name-text {
        color: var(--xy-primary) !important;
        font-weight: 600;
        padding: 6px 12px;
        border-radius: 8px;
        transition: background 0.3s;
    }

    .user-name-text:hover {
        background: rgba(249, 85, 2, 0.1);
    }

    .el-dropdown >>> .el-avatar {
        border: 2px solid var(--xy-primary);
        transition: transform 0.3s;
    }

    .el-dropdown:hover >>> .el-avatar {
        transform: scale(1.1);
    }

    /* 下拉菜单改造 */
    .el-dropdown-menu {
        border-radius: 12px !important;
        box-shadow: 0 8px 24px rgba(0,0,0,0.12) !important;
    }

    .el-dropdown-menu__item {
        font-size: 14px !important;
        padding: 12px 20px !important;
    }

    .el-dropdown-menu__item:hover {
        background: rgba(255,85,0,0.08) !important;
        color: var(--xy-primary) !important;
    }
</style>